/**
 * Created by Administrator on 17-9-29.
 */
var  cardD89=require('./cardD89');

var core_d89=function()
{

};

core_d89.prototype.shuffleCards0=function()
{
    var  list=cardD89.Card1();
    var  len=list.length;
    var  randList=[];
    var  map={};
    var   count=len*1000;
    var   acc=0;
    for(var i=0;i<count;i++)
    {   acc++;
        var  r=parseInt(Math.random() * len);
        if(map[r]==undefined)  {map[r]=1; randList.push(r);}
        else  map[r]++;
        var  n=randList.length;
        if(n==len) break;
    }
    console.log('acc=',acc);
    var  res=[];
    var   len1=len/2;
    for(var i=0;i<len1;i++)
    {
        var c1=list[randList[i*2]];
        var c2=list[randList[i*2+1]];
        var  cc=[c1,c2];
        res.push(cc);
    }
    return res;
};
core_d89.prototype.shuffleCards1=function(baoziCount)
{
    var  list=cardD89.Card1();
    var len=list.length;

    for(var j, x, i = len; i; j = parseInt(Math.random() * i), x = list[--i], list[i] = list[j], list[j] = x);
    var  res=[];
    var  n=len/2;
    for(var i=0;i<n;i++)
    {
        var c1=list[i*2];
        var c2=list[i*2+1];
        var  cc=[c1,c2];
        if(c1.dian==c2.dian)  baoziCount.n++;
        res.push(cc);
    }

    return res;
};

core_d89.prototype.shuffleCards=function()
{   var res;
    for(var  i=0;i<10;i++)
    {
        var  baoziCount={n:0};
       res=this.shuffleCards1(baoziCount);
        if(baoziCount.n<2)  return res;
    }
    return res;
}
core_d89.prototype.printCards=function(list)
{
    var  str='[';
    for(var i in list)
    {
        var card=list[i];
        str+=card.name;str+=',';
    }
    str+='] '
    return str;
};
core_d89.prototype.cards2List=function(cards)
{
    var  list=[];
    var  len=cards.length;
    if(!len) return;
    for(var i=0;i<len;i++)
    {   var  card=cards[i];
        var  it=[card.type-1,card.num];//花色和牌的大小
        list.push(it);
    }
    return list;
}

core_d89.prototype.makeCard=function(list)
{
    var  cards=[cardD89.map1[list[0]],cardD89.map1[list[1]]];
    if(!cards[0]) {console.log("no card ",list[0])}
    if(!cards[1]) {console.log("no card ",list[1])}
    return cards;
};

core_d89.prototype.getCardType=function(cardList,typeFanbei)
{
    var  mutiple=cardD89.cardsType.mutiple;
    if(typeFanbei==1) mutiple=cardD89.cardsType.mutiple1;
    var  cards=cardList.slice();
    cards.sort(function(a,b)
    {return b.num- a.num;});//按从大到小排序
    var  c1=cards[0],c2=cards[1];
    var  item={cards:cardList,type:cardD89.cardType.NORMAL,dian:c1.dian+c2.dian,score:0,beishu:mutiple.NORMAL};
    if(c1.dian==0.5&&c2.dian==0.5)
    {   item.type=cardD89.cardType.TIANBAO;
        item.beishu=mutiple.TIANBAO;
        return item}
    if(c1.dian==c2.dian)
    {   item.type=cardD89.cardType.BAO_ZI; item.beishu=mutiple.BAO_ZI;
        item.dian=c1.dian;
        return  item;}
    var  n1=c1.dian+c2.dian;
    var  n2=c1.dian*c2.dian;
    if(n1==9&&n2==8)
    {    item.type=cardD89.cardType.DING89;
        item.beishu=mutiple.DING89;
        return item;
    }
    var n=c1.dian+c2.dian;
    if(n==10)
    {   item.type=cardD89.cardType.CHOU_10;
        item.beishu=mutiple.CHOU_10;
        return item;}
    return item;
};

//庄家和所有闲家比牌
core_d89.prototype.compareAllCard=function(bank,guestList)
{
    var   b1=this.getCardType(bank);
    b1.dian1=b1.type+b1.dian%10;
    var len=guestList.length;
    var  result=[];
    for(var i=0;i<len;i++)
    {
        var b=this.getCardType(guestList[i]);
        b.dian1= b.type+ b.dian%10;
        if(b1.dian1== b.dian1&&b1.type== b.type&& b.type==cardD89.cardType.CHOU_10)
        {
            b1.score+=b1.beishu;b.score-=b1.beishu;
            console.logg('庄家闲家都是臭10，庄家赢',b1.beishu);
            continue;
        }
        if(b1.dian1> b.dian1)
        {
            b1.score+=b1.beishu;b.score-=b1.beishu;
            console.logg('庄家+',b1.beishu,'闲家-',b1.beishu);
        }
        if(b1.dian1< b.dian1)
        {
            b1.score-=b.beishu;b.score+=b.beishu;
            console.logg('庄家-',b.beishu,'闲家+',b.beishu);
        }
        result.push(b);
    }
    return {bank:b1,guests:result};
};

//庄家和所有闲家比牌
core_d89.prototype.compareAllByUser=function(bank,guestList,typeFanbei)
{
    bank.score=0;
    var len=guestList.length;
    for(var i=0;i<len;i++)
    {   var user=guestList[i];
        user.score=0;
    }
    var b1=this.getCardType(bank.cards,typeFanbei);
    b1.dian1=b1.type+b1.dian%10;
    var bankBet=bank.zbet;
    if(bankBet<=0||isNaN(bankBet)) bankBet=1;
    for(var i=0;i<len;i++)
    {
        var user=guestList[i];
        if(user.bet<=0||isNaN(user.bet))  user.bet=1;
        var b=this.getCardType(user.cards,typeFanbei);
        b.dian1= b.type+ b.dian%10;
        if(b1.dian1== b.dian1&&b1.type== b.type&& b.type==cardD89.cardType.CHOU_10)
        {   var nBei=b1.beishu*user.bet*bankBet;
            bank.score+=nBei;user.score-=nBei;
            console.logg('庄家闲家都是臭10，庄家赢',nBei);
            continue;
        }
        if(b1.dian1> b.dian1)
        {   var nBei=b1.beishu*user.bet*bankBet;
            bank.score+=nBei;user.score-=nBei;
            console.logg(bank.name+'庄家+',nBei,user.name+'闲家-',nBei);
            //return;//该死的return 导致别的玩家没有比较机会
        }
        if(b1.dian1< b.dian1)
        {
            var  nBei=b.beishu*user.bet*bankBet;
            bank.score-=nBei;user.score+=nBei;
            console.logg(bank.name+' 庄家-',nBei,user.name+' 闲家+',nBei);
            //return; 导致别的玩家没有比较机会
        }
        if(b1.dian1== b.dian1)
          console.logg('庄家闲家 打和');
    }

};

core_d89.prototype.TongBiRaw=function(userList,typeFanbei)
{

};
//通比
core_d89.prototype.TongBi=function(userList,typeFanbei)
{
    var len=userList.length;
    var  mutiple=cardD89.cardsType.mutiple;
    if(typeFanbei==1) mutiple=cardD89.cardsType.mutiple1;

    for(var i=0;i<len;i++)
    {
        var user=userList[i];
        var  info=this.getCardType(user.cards,typeFanbei);
        info.dian1=info.type+info.dian%10;
        user.cardTypeInfo=info;
        user.score=mutiple.NORMAL;
        if(info.type==cardD89.cardsType.TIANBAO)  user.score=mutiple.TIANBAO;
        if(info.type==cardD89.cardsType.BAO_ZI)  user.score=mutiple.BAO_ZI;
        if(info.type==cardD89.cardsType.DING89)  user.score=mutiple.DING89;
    }
    userList.sort(function(a,b)
    {
        //点数相同比较花色
        if(b.cardTypeInfo.dian1==a.cardTypeInfo.dian1)
        {
            var c1= a.cards;var c2= b.cards;
            var  ret1=c1[0].num*10+c1[0].type;
            var  ret2=c2[0].num*10+c2[0].type;
            return    ret2-ret1;
        }
        return b.cardTypeInfo.dian1- a.cardTypeInfo.dian1;
    });


};


module.exports=core_d89;
